
package org.japril.swing.jutil.logging2;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.japril.jutil.logging.ControlLogSink;
import org.japril.jutil.logging.ControlLogSink.ClassLogInfo;
import org.japril.swing.jutil.logging.ControlLogSinkClassInfoTableModel;
import org.japril.swing.jutil.logging.ControlLogSinkTableModel;
import org.japril.swing.jutil.logging.ControlLogSinks;

@SuppressWarnings("serial")
public final class ControlLogSinkPanel2 extends JPanel 
{
  private final JTable m_sTable=
    new JTable(ControlLogSinkTableModel.EMPTY_MODEL);
    
  private final JTable m_cTable=
    new JTable(ControlLogSinkClassInfoTableModel.EMPTY_MODEL);
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ControlLogSinkPanel2()
  {
    setLayout(new BorderLayout());
    
    m_sTable.getSelectionModel().addListSelectionListener
    (
      new ListSelectionListener()
      {
        public void valueChanged(ListSelectionEvent e)
        {
          int row=m_sTable.getSelectedRow();
          if (row==-1)
          {
            m_cTable.setModel(ControlLogSinkClassInfoTableModel.EMPTY_MODEL);
          }
          else
          {
            ControlLogSinkTableModel m=(ControlLogSinkTableModel)
              m_sTable.getModel();
            ClassLogInfo cli=m.getClassLogInfo(row);
            m_cTable.setModel(new ControlLogSinkClassInfoTableModel(cli));
          }
        }
      }
    );
    
    ControlLogSinks managers=ControlLogSinks.getInstance();
    if (managers!=null)
    {
      managers.addChangeListener
      (
        new ChangeListener()
        {
          public void stateChanged(ChangeEvent e){ update(); }
        }
      );
    }
    
    JSplitPane sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    sp.setLeftComponent(new JScrollPane(m_sTable));
    sp.setRightComponent(new JScrollPane(m_cTable));
    sp.setDividerLocation(300);
    
    add(sp, BorderLayout.CENTER);
    
    makeToolBars();    
    
    update();
  }
  
  private void makeToolBars()
  {
    JPanel north=new JPanel(new FlowLayout(FlowLayout.LEFT));
    
    JToolBar bar=new JToolBar();
    bar.add(m_updateAction);
    north.add(bar);
    
    north.add(new ControlLogSinksBar());
    
    add(north, BorderLayout.NORTH);
  }
  
  private final Action m_updateAction=new AbstractAction("Update")
  { 
    public void actionPerformed(ActionEvent e){ update(); }
  };
  
  private void update()
  {
    m_sTable.setModel(ControlLogSinkTableModel.EMPTY_MODEL);
    m_sTable.setModel(ControlLogSinkClassInfoTableModel.EMPTY_MODEL);
    ControlLogSinks sinks=ControlLogSinks.getInstance();
    if (sinks!=null)
    {
      ControlLogSink sink=sinks.getCurrentSink();
      
      System.out.println("ControlLogSinkPanel2: set current: "+sink.getName());
      
      m_sTable.setModel(new ControlLogSinkTableModel(sink));
    }
  }
  
}
